﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System.Web;
using System.Configuration;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 跟单猿TMS_同步WMS
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WaybillInfoSendWMSController : ControllerBase
    {
        private readonly ILogger<WaybillInfoSendWMSRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_gz;


        public WaybillInfoSendWMSController(ILogger<WaybillInfoSendWMSRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_gz = _configuration.GetConnectionString("WMSDbConnection_GZ");

        }

        /// <summary>
        /// 跟单猿TMS_同步WMS
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWaybillInfoSendWMS([FromBody] WaybillInfoSendWMSRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WaybillInfoSendWMSResponseDto re = new WaybillInfoSendWMSResponseDto();
            WaybillInfoSendWMSRequestDto pus = new WaybillInfoSendWMSRequestDto();
            
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("organization", "");//查询参数
                DataTable push_dt = MethodHelp.GetTableInfo("TMS0032", dic, con);//获取推送数据
                if (push_dt.Rows.Count > 0)
                {
                    foreach (DataRow item in push_dt.Rows)
                    {
                        strSqlList = new List<string>();
                        string wms_con = "";
                        string factory_code = "";
                        if (item["deliverCompanyName_start"].ToString().Contains("金湾高景"))
                        {
                            wms_con = con_zh;
                            factory_code = "C01";
                        }
                        else if (item["deliverCompanyName_start"].ToString().Contains("青海高景"))
                        {
                            wms_con = con_qh;
                            factory_code = "B01";
                        }
                        else if (item["deliverCompanyName_start"].ToString().Contains("四川高景"))
                        {
                            wms_con = con_yb;
                            factory_code = "E01";
                        }
                        else if (item["deliverCompanyName_start"].ToString().Contains("高臻"))
                        {
                            wms_con = con_gz;
                            factory_code = "G01";
                        }                       
                        else
                        {

                        }

                        if (wms_con!="")
                        {
                            dic = new Dictionary<string, string>();
                            dic.Add("factory_code", factory_code);
                            dic.Add("shippingorder_no", item["transportOrderNo"].ToString());
                            dic.Add("shipping_date", item["requiredTime"].ToString());
                            dic.Add("carrier_code", item["carrierCode"].ToString());
                            dic.Add("carrier_name", item["carrierName"].ToString());
                            dic.Add("car_no", item["planeNumber"].ToString());
                            dic.Add("driver_name", item["driverMaName"].ToString());
                            dic.Add("driver_tel", item["driverMaPhone"].ToString());
                            dic.Add("sales_order_no", item["saleNo"].ToString());
                            dic.Add("customer_code", item["deliverCompanyCode_end"].ToString());
                            dic.Add("customer_name", item["deliverCompanyName_end"].ToString());
                            dic.Add("sales_order_line_id", item["subOrderNo"].ToString());
                            dic.Add("sales_order_line_no", item["subOrderNo"].ToString());
                            dic.Add("material_code", item["goodsCode"].ToString());
                            dic.Add("material_name", item["goodsName"].ToString());
                            dic.Add("material_amount", item["goodsTotal"].ToString());
                            dic.Add("shipment_amount", item["goodsTotal"].ToString());
                            dic.Add("start_address", item["loadAddress_start"].ToString());
                            dic.Add("target_address", item["loadAddress_end"].ToString());
                            dic.Add("receipt_man", item["deliverUserName_end"].ToString());
                            dic.Add("receipt_tel", item["deliverUserPhone_end"].ToString());
                            dic.Add("plan_id", item["logisticsConfigId"].ToString());
                            dic.Add("create_by", item["createUserNumber"].ToString());
                            dic.Add("crm_no", item["orderNo"].ToString());
                            dic.Add("receipt_no", item["transportOrderNo"].ToString()+ item["deliverCompanyCode_end"].ToString());
                            dic.Add("address_id", item["transportOrderLoadId"].ToString());
                            string strSQL = MethodHelp.GetTranSQL("TMS0033", dic, con);
                            if (strSQL.Trim() != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "TMS0030 Parameter conversion failed";
                            }

                            if (strMsg == "")
                            {
                                if (strSqlList.Count > 0)
                                {
                                    bool A = DB.ExecuteNoQueryTran(strSqlList, wms_con);
                                    if (A)
                                    {
                                        dic = new Dictionary<string, string>();
                                        dic.Add("id", item["id"].ToString());
                                        strMsg = MethodHelp.InsertIntoTable("TMS0034", dic,con);

                                    }


                                }
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.code = 0;
               
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
